#include "DirectX.h"


DirectX::DirectX(void)
{
	this->_d3d = NULL;
	this->_d3ddev = NULL;
	this->_hWnd  = NULL;
	this->_backbuffer = NULL;
	this->_spriteHandler = NULL;
}


DirectX::~DirectX(void)
{
	if (_d3ddev != NULL)
		_d3ddev->Release();

	if (_d3d != NULL)
		_d3d->Release();
}

int DirectX::InitDirect3D(int ScreenWidth, int ScreenHeight)
{
	//Initialize the Direct3D
	_d3d = Direct3DCreate9(D3D_SDK_VERSION);

	if (_d3d == NULL)
	{
		MessageBox(NULL,"Error Initialize Direct3D","Error",MB_OK);
		return 0;
	}

	D3DPRESENT_PARAMETERS d3dpp;

	ZeroMemory(&d3dpp,sizeof(d3dpp));
	d3dpp.Windowed = !false;
	d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	d3dpp.hDeviceWindow = _hWnd;
	d3dpp.BackBufferCount = 1;
	d3dpp.BackBufferWidth = ScreenWidth;
	d3dpp.BackBufferHeight = ScreenHeight;

	_d3d->CreateDevice(
		D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL,
		_hWnd,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING,
		&d3dpp,
		&_d3ddev);

	if (_d3ddev == NULL)
	{
		MessageBox(NULL,"Error Creating D3Ddevice","Error",MB_OK);
		return 0;
	}

	_backbuffer = NULL;
	_d3ddev->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&_backbuffer);


	//Initialize Sprite Handler
	HRESULT result = D3DXCreateSprite(_d3ddev,&_spriteHandler);
	if (result != D3D_OK)
	{
		MessageBox(NULL,"Error Creating D3DXSprite!","Error",MB_OK);
		return 0;
	}

	return 1;
}

LPDIRECT3DSURFACE9 DirectX::getBackBuffer()
{
	return _backbuffer;
}

LPDIRECT3DDEVICE9 DirectX::getDevice()
{
	return _d3ddev;
}

void DirectX::setHandle(HWND hWnd)
{
	_hWnd = hWnd;
}

LPD3DXSPRITE DirectX::getSpriteHandler()
{
	return _spriteHandler;
}

LPDIRECT3DSURFACE9 DirectX::LoadSurface(LPCSTR filePath,D3DCOLOR transparentColor)
{
	HRESULT result;
	LPDIRECT3DSURFACE9 surface = NULL;
	D3DXIMAGE_INFO info;

	result = D3DXGetImageInfoFromFile(filePath,&info);
	if (result != D3D_OK)
		return NULL;

	result = _d3ddev->CreateOffscreenPlainSurface(info.Width,info.Height,D3DFMT_X8R8G8B8,D3DPOOL_DEFAULT,&surface,NULL);
	if (result != D3D_OK)
		return NULL;

	result = D3DXLoadSurfaceFromFile(surface,NULL,NULL,filePath,NULL,D3DX_DEFAULT,transparentColor,&info);
	if (result != D3D_OK)
		return NULL;

	return surface;
}

LPDIRECT3DTEXTURE9 DirectX::LoadTexture(LPCSTR filePath,D3DCOLOR transparentColor)
{
	LPDIRECT3DTEXTURE9 texture = NULL;
	D3DXIMAGE_INFO info;
	HRESULT result;

	result = D3DXGetImageInfoFromFile(filePath,&info);
	if (result != D3D_OK)
	{
		MessageBox(NULL,"Can not get image Info!","Error",MB_OK);
		return NULL;
	}

	result = D3DXCreateTextureFromFileEx(_d3ddev,
										filePath,
										info.Width,
										info.Height,
										1,
										D3DPOOL_DEFAULT,
										D3DFMT_UNKNOWN,
										D3DPOOL_DEFAULT,
										D3DX_DEFAULT,
										D3DX_DEFAULT,
										transparentColor,
										&info,
										NULL,
										&texture);
	if (result != D3D_OK)
	{
		MessageBox(NULL,"Load texture error!","Error",MB_OK);
		return NULL;
	}
	return texture;
}

void DirectX::RenderSurface(LPDIRECT3DSURFACE9 surface)
{
	_d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &_backbuffer);
	_d3ddev->StretchRect(surface,NULL,this->_backbuffer,NULL,D3DTEXF_NONE);
}